package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.plus.web.controller.BasePlusController;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.system.domain.SysMenuBranch;
import com.ruoyi.system.domain.form.SysMenuBranchForm;
import com.ruoyi.system.domain.params.SysMenuBranchParams;
import com.ruoyi.system.service.ISysMenuBranchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 分支菜单权限Controller
 *
 * @author liuxing
 * @date 2021-01-23
 */
@RestController
@RequestMapping("/system/sys_menu_branch")
public class SysMenuBranchController extends BasePlusController {
    @Autowired
    private ISysMenuBranchService sysMenuBranchService;

    /**
     * 查询分支菜单权限列表
     */
    @PreAuthorize("@ss.hasPermi('system:system:sys_menu_branch:list')")
    @GetMapping("/list")
    public AjaxResult list(SysMenuBranchParams sysMenuBranchParams) {
        List<SysMenuBranch> list = sysMenuBranchService.selectSysMenuBranchList(sysMenuBranchParams);
        return AjaxResult.success(list);
    }

    /**
     * 导出分支菜单权限列表
     */
    @PreAuthorize("@ss.hasPermi('system:system:sys_menu_branch:export')")
    @Log(title = "分支菜单权限", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMenuBranchParams sysMenuBranchParams) throws IOException {
        List<SysMenuBranch> list = sysMenuBranchService.selectSysMenuBranchList(sysMenuBranchParams);
        ExcelUtil<SysMenuBranch> util = new ExcelUtil<SysMenuBranch>(SysMenuBranch.class);
        util.exportExcel(response, list, "sysMenuBranch");
    }

    /**
     * 获取分支菜单权限详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:system:sys_menu_branch:query')")
    @GetMapping(value = "/{menuId}")
    public AjaxResult getInfo(@PathVariable("menuId") Long menuId) {
        return AjaxResult.success(sysMenuBranchService.selectSysMenuBranchById(menuId));
    }

    /**
     * 新增分支菜单权限
     */
    @PreAuthorize("@ss.hasPermi('system:system:sys_menu_branch:add')")
    @Log(title = "分支菜单权限", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody SysMenuBranchForm sysMenuBranchForm) {
        return R.ok(sysMenuBranchService.insertSysMenuBranch(sysMenuBranchForm));
    }

    /**
     * 新增分支菜单权限
     */
    /*@PreAuthorize("@ss.hasPermi('system:system:sys_menu_branch:add')")
    @Log(title = "分支菜单批量添加", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult batchAdd(@RequestBody SysMenuBranch sysMenuBranch)
    {
        //通过codes批量获取

        return toAjax(sysMenuBranchService.batchInsertSysMenuBranch(sysMenuBranch));
    }*/


    /**
     * 修改分支菜单权限
     */
    @PreAuthorize("@ss.hasPermi('system:system:sys_menu_branch:edit')")
    @Log(title = "分支菜单权限", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody SysMenuBranch sysMenuBranch) {
        return R.ok(sysMenuBranchService.updateSysMenuBranch(sysMenuBranch));
    }

    /**
     * 删除分支菜单权限
     */
    @PreAuthorize("@ss.hasPermi('system:system:sys_menu_branch:remove')")
    @Log(title = "分支菜单权限", businessType = BusinessType.DELETE)
    @DeleteMapping("/{menuIds}")
    public R remove(@PathVariable Long[] menuIds) {
        return R.ok(sysMenuBranchService.deleteSysMenuBranchByIds(menuIds));
    }

    /**
     * 启用分支菜单权限
     */
    @PreAuthorize("@ss.hasPermi('system:system:sys_menu_branch:enable')")
    @Log(title = "分支菜单权限", businessType = BusinessType.UPDATE)
    @PutMapping("/enable/{menuId}")
    public R enable(@PathVariable Long menuId) {
        return R.ok(sysMenuBranchService.enableSysMenuBranch(menuId));
    }

    /**
     * 停用分支菜单权限
     */
    @PreAuthorize("@ss.hasPermi('system:system:sys_menu_branch:stop')")
    @Log(title = "分支菜单权限", businessType = BusinessType.UPDATE)
    @PutMapping("/stop/{menuId}")
    public R stop(@PathVariable Long menuId) {
        return R.ok(sysMenuBranchService.stopSysMenuBranch(menuId));
    }
}
